package com.liyiruo.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Proxy;

/**
 * bean后置处理器 增强日志功能
 */
public class TimeLogBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    Object obj=  Proxy.newProxyInstance(
                bean.getClass().getClassLoader(),
                bean.getClass().getInterfaces(),
                (proxy,method,args)-> {
                    System.out.println("开始时间=》"+System.currentTimeMillis());
                    Object invoke = method.invoke(bean, args);
                    System.out.println("结束时间=》"+System.currentTimeMillis());
                    return invoke;
                }
        );
        return obj;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
